import { mapGetters } from 'vuex'
import { compareVersion } from '@/utils'
import EquityList from '@/components/member/equity-list'
import EquityTabs from '@/components/member/equity-tabs'
import { getPackageByPidV2, getPackagesList } from '@/api/rights'
export default {
  components: { EquityList, EquityTabs },
  data () {
    return {
      pkgH: 0,
      packages: {},
      packagesList: []
    }
  },
  computed: {
    ...mapGetters(['appInfo'])
  },
  created () {
    this.pkgid = this.$route.query.pkgid || '10000'
  },
  methods: {
    getPackagesList () {
      const params = {
        pid: this.pkgid.split(',')
      }
      getPackagesList(params).then(res => {
        let num = 0
        this.packagesList = (res.data?.list || []).map(it => {
          it.cat_list = (it.cat_list || []).map(item => {
            item.right_list = this.filtersRights(item.right_list)
            num = Math.max(Math.min(item.right_list.length, 6), num)
            return item
          })
          return it
        })
        this.pkgH = num * 22 + (num - 1) * 12
        this.onLoadFinish && this.onLoadFinish()
      })
    },
    getPackages () {
      getPackageByPidV2({ pid: this.pkgid }).then(res => {
        const data = res.data || {}
        data.cat_list = (data.cat_list || []).map(item => {
          item.right_list = this.filtersRights(item.right_list)
          return item
        })
        data.rights = this.getRightsList(data.cat_list)
        this.packages = data
        this.onLoadFinish && this.onLoadFinish()
      })
    },
    getRightsList (list = []) {
      let arr = []
      let num = 0
      for (const item of list) {
        const len = Math.min(item.right_list.length, 6)
        if (len > num) {
          num = len
        }
        arr = arr.concat(item.right_list || [])
      }
      this.pkgH = num * 22 + (num - 1) * 12
      return arr
    },

    filtersRights (list = []) {
      return list.filter(item => {
        return compareVersion(this.appInfo.version, item.min_ver) !== -1
      })
    }
  }
}
